package com.util;

import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;


class SysConfig {
	private static final String CONFIG = "config.properties";
	private static final String GLOBAL="global.properties";// updated by Michael, Mar 30, 2012, for:add global config.
	private static Properties p;

	static {
		try {
		    InputStream input2 = ConfigHelper.getConfigStream(GLOBAL);
			InputStream input = ConfigHelper.getConfigStream(CONFIG);			
			p = new Properties();
			p.load(input2);
			p.load(input);//put this at last priority, this will overwrite upper config
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getItem(String key) {
		 key = key.toLowerCase();
		 String result =p.getProperty(key, "");
		 if(StringUtils.isEmpty(result)){
			 key = key.toUpperCase();
			 result =p.getProperty(key,"");
		 }
		 return result; 
	}
	
	public static Integer getInteger(String key) {
		String value = SysConfig.getItem(key);
		Integer result = null;
		if (value != null && value.length() > 0) {
			result = Integer.parseInt(value);
		}
		return result;
	}
	
	public static Boolean getBoolean(String key) {
		String value = SysConfig.getItem(key);
		Boolean result = false;
		if (value != null && value.length() > 0) {
			if(value.trim().equals("true")){
				result = true;
			}
		}
		return result;
	}
}
